package com.yakami.ocean.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding.view.RxView;
import com.yakami.ocean.R;
import com.yakami.ocean.bean.TestItem;
import com.yakami.ocean.event.Event;
import com.yakami.ocean.event.RxBus;
import com.yakami.ocean.view.fragment.base.BaseFragment;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author Yakami, Created on 2016/4/21
 */
public class TestFragment extends BaseFragment {

    @Bind(R.id.problem) TextView mProblem;
    @Bind(R.id.option_a) RadioButton mOptionA;
    @Bind(R.id.option_b) RadioButton mOptionB;
    @Bind(R.id.option_c) RadioButton mOptionC;
    @Bind(R.id.option_d) RadioButton mOptionD;
    @Bind(R.id.btn_next_problem) TextView mBtnNext;
    @Bind(R.id.option_group) RadioGroup mRadioGroup;
    private TestItem mTestItem;
    private Toast mToast;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceBundle) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        ButterKnife.bind(this, view);
        if (mTestItem != null) {
            mProblem.setText(mTestItem.title);
            mOptionA.setText(getOption(0));
            mOptionB.setText(getOption(1));
            mOptionC.setText(getOption(2));
            mOptionD.setText(getOption(3));
            RxView.clicks(mBtnNext)
                    .throttleFirst(500, TimeUnit.MILLISECONDS)
                    .subscribe((Void) -> {
                        if (mTestItem.selected != -1) {
                            if (mToast != null)
                                mToast.cancel();
                            Event event = new Event();
                            event.type = Event.EventType.NEXT_PROBLEM;
                            event.message = mTestItem;
                            RxBus.getInstance().send(event);
                        } else {
                            mToast = Toast.makeText(mAppContext, mRes.getText(R.string.please_make_a_choose).toString(), Toast.LENGTH_SHORT);
                            mToast.show();
                        }
                    });
        }
        mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> selectOption(checkedId));
        return view;
    }

    private void selectOption(int checkedId) {
        switch (checkedId) {
            case R.id.option_a:
                mTestItem.selected = 1;
                break;
            case R.id.option_b:
                mTestItem.selected = 2;
                break;
            case R.id.option_c:
                mTestItem.selected = 3;
                break;
            case R.id.option_d:
                mTestItem.selected = 4;
                break;
        }
    }

    private String getOption(int i) {
        if (mTestItem.options.size() > i) {
            return mTestItem.options.get(i);
        } else return "";
    }

    public void setTestItem(TestItem item) {
        mTestItem = item;
    }

}
